//
//  ListDataManager.m
//  ViperDemo
//
//  Created by Leeesven on 17/5/19.
//  Copyright © 2017年 Leeesven. All rights reserved.
//

#import "ListDataManager.h"

#import "NSCalendar+CalendarAdditions.h"
#import "NSArray+HOM.h"

#import "CoreDataStore.h"
#import "ManagedTodoItem.h"

#import "TodoItem.h"

@implementation ListDataManager

-(void)todoItemsBetweenStartDate:(NSDate *)startDate endDate:(NSDate *)endDate completionBlock:(void (^)(NSArray *))completionBlock{
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", [calendar dateForBeginningOfDay:startDate], [calendar dateForEndOfDay:endDate]];
    NSArray *sortDescriptors = @[];
    
    __weak typeof(self) welf = self;
    [self.dataStore
     fetchEntriesWithPredicate:predicate
     sortDescriptors:sortDescriptors
     completionBlock:^(NSArray *entries) {
         if (completionBlock)
         {
             completionBlock([welf todoItemsFromDataStoreEntries:entries]);
         }
     }];
}

- (NSArray *)todoItemsFromDataStoreEntries:(NSArray *)entries
{
    return [entries arrayFromObjectsCollectedWithBlock:^id(ManagedTodoItem *todo) {
        return [TodoItem todoItemWithDueDate:todo.date name:todo.name];
    }];
}

@end
